/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gunncs.actoriface;

import com.sun.spot.peripheral.II2C;
import com.sun.spot.sensorboard.EDemoBoard;
import java.io.IOException;


/**
 *
 * @author anand
 */
public class Compass extends Sensor{
	
	public static final byte SLA_HMC6352_READ = (byte)0x42;
	public static final byte SLA_HMC6352_WRITE = (byte)0x41;
	//private EDemoBoard eDemo;
	private II2C i2cDevice;
	public Compass(II2C e){
		i2cDevice = e;
		double[] d = new double[2];
		try{
			i2cDevice.open();
			d[0] = 0;
		}catch(Exception ex){
			d[0] = 255;
			//e.printStackTrace();
		}
		d[1] = Simulator.time();
		setState(d);
	}
	
	/*public void sleep(){
		try{
			byte[] command = new byte[1];
			command[0] = (byte)('S');
			i2cDevice.write(SLA_HMC6352_WRITE, command[0], 1);	
		}catch(Exception ex){
			e.printStackTrace();
		}
	}*/
	
	public synchronized void update(Thread daemon){
        
    double[] d = new double[2];
		byte[] command = new byte[1];
		command[0] = (byte)('A');
		byte[] data = new byte[2];
		try{
			i2cDevice.read(SLA_HMC6352_READ, command[0], 1, data, 0, 2);
			d[0] = ((((data[0]) << 8) + ( data[1])) / 10); //(((0xFF & data[0]) << 8) | ((0xFF) & data[1]));
			//System.out.println("Compass " +d[0]);
			d[1] =(double)Simulator.time();
		}catch(Exception e){
		  //e.printStackTrace();
			//System.out.println("Compass reading failed");
		}
        //Simulator.out.println("                   " +d[0]);
        setState(d);
  }
	
	  public String getName(){
        return "Compass";
    }
    
    public int getID(){
      return 4;
    }
    
}